package algoritmos;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import beans.Cliente;
import beans.Concessionaria;

public class HashJoin extends Join{

	public HashJoin(List<Cliente> clientes, List<Concessionaria> concessionaria) {
		super(clientes, concessionaria);
	}
	
	@Override
	public String[][] orderna() {
		//Parte 1
		Map<Integer, List<List<String>>> map = new HashMap<Integer, List<List<String>>>();
		map.put(0, new ArrayList<List<String>>());
		map.put(1, new ArrayList<List<String>>());
		map.put(2, new ArrayList<List<String>>());
		
		for(int i=0, max=clientes.size(); i<max; i++){
			map.get(doHash(clientes.get(i).getCod())).add(clientes.get(i).getSerializedData());
		}
		
		List<List<String>> matriz = new ArrayList<List<String>>();
		List<String> line;
		
		//Parte 2
		for(int i=0, max=map.size(); i<max; i++){
			for(int j=0,max2=concessionaria.size();j<max2;j++){
				for(List<String> tupla:map.get(i)){
					if(concessionaria.get(j).getCodCliente() == Integer.parseInt(tupla.get(0))){
						line = tupla;
						line.addAll(concessionaria.get(j).getSerializedData());
						matriz.add(line);
					}
				}
			}
		}
		
		return parseToMatrix(matriz);
	}

	private int doHash(int indice){
		return indice%3;
	}
}
